package cfca.seal.web.tag;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.SealImage;
import cfca.seal.dao.main.ISealImageDao;
import cfca.seal.web.system.SessionManager;
import cfca.seal.web.tag.base.TemplateDirectiveModelBase;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class SealModelSelectTag extends TemplateDirectiveModelBase
{

  @Resource
  ISealImageDao dao;

  public String process(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException
  {
    AdminUser manager = SessionManager.getManager(this.request);
    List list = null;
    String organizationId = manager.getUserType() > 0 ? manager.getOrganizationId() : "";
    try {
      list = this.dao.getAllForSelectShow(organizationId);
    } catch (Exception e) {
      e.printStackTrace();
    }

    StringBuffer sf = new StringBuffer();
    sf.append("<select size=\"1\" class=\"").append(this.className).append("\" id=\"").append(this.elementId).append("\" name = \"").append(this.elementName).append("\" >");

    sf.append("<option value=\"\">--请选择--</option>");

    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        SealImage sealmodel = (SealImage)list.get(i);
        sf.append("<option value=\"").append(sealmodel.getId()).append("\" ");

        if (sealmodel.getId().equals(this.elementValue)) {
          sf.append("selected = \"selected\"");
        }

        sf.append(" >").append(sealmodel.getCode() + "-" + sealmodel.getName()).append("</option>");
      }
    }

    sf.append("</select>");
    return sf.toString();
  }
}